﻿<%@ Page Title="" Language="C#" MasterPageFile="~/masterpage.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UL360SurveyWeb.Default" culture="auto" meta:resourcekey="PageResource1" uiculture="auto" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="headcontent" runat="server">
    <link href="<%= Page.ResolveUrl("~/Styles/Survey.css") %>" rel="Stylesheet" type="text/css" />
    <script type="text/javascript"  src="<%= Page.ResolveUrl("~/Scripts/jquery.js") %>"></script>
    <script type="text/javascript" src="<%= Page.ResolveUrl("~/Scripts/charCount.js") %>"></script>
    <script type="text/javascript">

        
        function send_click(clicked_id) {
            //Get all radio button elements
            var radiobutton = document.getElementsByClassName("radiobuttons");

            //Get scale questions
            var exisitingScaleQuestions = document.getElementsByClassName("scale-questionstyle");
            var q = exisitingScaleQuestions.length;
            //Array to hold scale question id:s with the size of exisiting scale questions
            var id = new Array(q);
            //Array to hold scale question values
            var answers = new Array(q);

            //Get text question id to be able to get text
            var txtQuestionId = document.getElementsByClassName("text-labels");
            var t = txtQuestionId.length;
            //Array to hold scale question id:s with the size of exisiting scale questions
            var textid = new Array(t)
            //Add question numbers to array to get group name
            for (var txt = 0; txt < t; txt++) {
                textid[txt] = txtQuestionId[txt].innerHTML;
            }

            //Lookup if there is any text written and if so add the id to an array and the text to another array both with the same index
            var index = 0;

            //Arrays to hold the text and the question id for the text
            var tqid = new Array(index);
            var texts = new Array(index);

            for (var n = 0; n < t; n++) {
                var textfield = document.getElementById(textid[n]);
                if (textfield.value.trim() != "") {
                    tqid[index] = textid[n];
                    texts[index] = "#####" + textfield.value;
                    index++;
                }
            }

            //Make arrays to string
            var textids = tqid.toString();
            var textanswers = texts.toString();

            //Set hidden field values to answers and ids to be able to get them from code behind
            document.getElementById("TextIDs").value = textids;
            document.getElementById("TextAnswers").value = textanswers;
            
            //Add names to array from repeater label with question number to get the radiobuttons group name and length
            var questionId = document.getElementsByClassName("scale-labels");
            var questionsLength = questionId.length;

            //Add question numbers to array to get group name
            for (var x = 0; x < questionsLength; x++) {
                id[x] = questionId[x].innerHTML;
            }

            //Looking up if radiobuttons in each group is checked or not
            first: for (var l = 0; l < id.length; l++) {
                var group = document.getElementsByName(id[l]);
                second: for (var i = 0; i < group.length; i++) {
                    if (group[i].checked == true) {
                        continue first;
                    }
                }
                //If a scale question is not answered
                var valueAtIndex = l;

                //Get alert messages from local resource
                var alertMessage1 = '<%= GetLocalResourceObject("RadioButtonAlert1.Text") %>';
                var alertMessage2 = '<%= GetLocalResourceObject("RadioButtonAlert2.Text") %>';

                //Alert message when a scale question is not answered
                alert(alertMessage1 + (valueAtIndex + 1) + alertMessage2);

                //Prevent reload of page when alert button is clicked
                event.returnValue = false;
                return false;
            }

            //alert(JSON.stringify(answers));

            //If textarea characters are too many
            var textArea = document.getElementsByTagName("textarea");
            var alertMsgTextAreas = '<%= GetLocalResourceObject("alertMsgTextAreas.Text") %>';
            for (var t = 0; t < textArea.length; t++) {
                if (textArea[t].value.length > 500) {
                    alert(alertMsgTextAreas);
                    event.returnValue = false;
                    return false;
                }
            }

            //Add radio button values to answers array
            for (var y = 0; y < id.length; y++) {
                var value = $("input:radio[name='" + id[y] + "']:checked").val();
                answers[y] = value;
            }

            var answersx = answers.toString();
            document.getElementById("ScaleAnswers").value = answersx;
        }

        //Count characters in textareas
        $(document).ready(function () { 
            $("textarea").charCount({ 
                allowed: 500,		
                warning: 50,
            });
        });

    </script>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="header" runat="server">
    <div style="float: left">
        <asp:Image ID="Image1" runat="server" CssClass="logo" ImageUrl="~/Images/logotyp-forsvarshogskolan-webb-sve.jpg" meta:resourcekey="Image1Resource1" />
    </div>
    <div style="float: left; margin-left: 40px;">
        <h1>
            <span id="rubrik">Kursutvärdering</span>
        </h1>
    </div>
    <div style="float: right">
        <asp:Image ID="Logo" runat="server" CssClass="logoUL" ImageUrl="~/Images/UL logo.jpg" meta:resourcekey="LogoResource1" />
    </div>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <div class="content-container">
        <asp:HiddenField ID="ParticipantID" runat="server" ClientIDMode="Static" />
        <asp:HiddenField ID="FormID" runat="server" ClientIDMode="Static" />
        <asp:HiddenField ID="SurveyType" runat="server" ClientIDMode="Static" />
        <asp:HiddenField ID="CourseID" runat="server" ClientIDMode="Static"/>
        <asp:HiddenField ID="ScaleAnswers" runat="server" ClientIDMode="Static" />
        <asp:HiddenField ID="TextIDs" runat="server" ClientIDMode="Static" />
        <asp:HiddenField ID="TextAnswers" runat="server" ClientIDMode="Static" />
        
            <div class="survey-container">
                <h2>
                    <asp:Label ID="SurveyHeading" runat="server" Text="" meta:resourcekey="SurveyHeading"></asp:Label>
                    <asp:Label ID="CoursePart1" Visible="false" runat="server" Text="" meta:resourcekey="CoursePart1"></asp:Label>
                    <asp:Label ID="CoursePart2" Visible="false" runat="server" Text="" meta:resourcekey="CoursePart2"></asp:Label>
                </h2>
                
                <asp:Label CssClass="info-title" ID="CourseNameTitle" runat="server" Text="" meta:resourcekey="CourseName"></asp:Label>
                <asp:Label ID="CourseName" runat="server" Text=""></asp:Label>
                <br />
                <asp:Label CssClass="info-title" ID="CourseTitle" runat="server" Text="" meta:resourcekey="Course"></asp:Label>
                <asp:Label ID="CourseDate" runat="server" Text=""></asp:Label>
                <br />
                <asp:Label CssClass="info-title" ID="TutorTitle" runat="server" Text="" meta:resourcekey="CourseTutor"></asp:Label>
                <asp:Label ID="TutorName" runat="server" Text="" meta:resourcekey="TutorName"></asp:Label>
                <br />

            <!-- Skalfrågorna -->
                <div id="ScaleQuestionsContainer" runat="server">
                    <h5><asp:Label ID="Label1" runat="server" Text="Frågor" meta:resourcekey="ScaleQuestionTitle"></asp:Label><br />
                        <asp:Label CssClass="description" ID="Label2" runat="server" Text="Skala 1-9 där 1 = Lite och 9 = Mycket" meta:resourcekey="ScaleQuestionDescription"></asp:Label>
                    </h5>
                    <asp:Repeater ClientIDMode="Static" ID="ScaleSurveyQuestions" runat="server">
                    <HeaderTemplate>
                        <ul>
                    </HeaderTemplate>
                        <ItemTemplate>
                            <asp:HiddenField ID="ScaleQuestionTextId" runat="server" Value='<%# Eval("SurveyQuestionTextId") %>'/>
                            <label class="scale-labels"><%# Eval("SurveyQuestionTextId") %></label>
                            <li>
                                <div>
                                    <div class="scale-questionstyle">
                                        <asp:Label CssClass="scale-question-number" ID="ScaleQuestionOrder" runat="server" Text=""><%# Container.ItemIndex + 1 %>.</asp:Label>
                                        <asp:Label ID="ScaleQuestion" runat="server" Text='<%# Eval("Question") %>' meta:resourcekey="QuestionResource1"></asp:Label></div>
                                    <% for(int i = 1; i <= 9; i++) { %>
                                        <label for="<%# Eval("SurveyQuestionTextId") %>#<% Response.Write(i.ToString()); %>"><% Response.Write(i.ToString()); %></label>
                                        <input class="radiobuttons" id="<%# Eval("SurveyQuestionTextId") %>#<% Response.Write(i.ToString()); %>" type="radio" value="<% Response.Write(i.ToString()); %>" name="<%# Eval("SurveyQuestionTextId") %>"/>
                                    <% }%>
                                    <label for="<%# Eval("SurveyQuestionTextId") %>#0"><%= GetLocalResourceObject("RadioButtonText.Text") %></label>
                                    <input class="radiobuttons" id="<%# Eval("SurveyQuestionTextId") %>#0" type="radio" value="0" name="<%# Eval("SurveyQuestionTextId") %>" />
                                
                                </div>
                            </li>
                        </ItemTemplate>
                    <FooterTemplate>
                        </ul>
                    </FooterTemplate>
                    </asp:Repeater>
                </div>
            <!-- Fritextfrågorna -->
                <div id="TextQuestionsContainer" runat="server">
                    <h5><asp:Label ID="Label3" runat="server" Text="Fritextfrågor" meta:resourcekey="TextQuestionTitle"></asp:Label><br />
                        <asp:Label CssClass="description" ID="Label4" runat="server" Text="(frivilliga att fylla i)" meta:resourcekey="TextQuestionDescription"></asp:Label>
                    </h5>
                    <asp:Repeater ClientIDMode="Static" ID="TextSurveyQuestions" runat="server">
                    <HeaderTemplate>
                        <ul>
                    </HeaderTemplate>
                        <ItemTemplate>
                            <asp:HiddenField ID="TextQuestionTextId" runat="server" Value='<%# Eval("SurveyQuestionTextId") %>'/>
                            <label class="text-labels"><%# Eval("SurveyQuestionTextId") %></label>
                            <li>
                                <div>                          
                                    <asp:Label CssClass="text-questionstyle" ID="TextQuestion" runat="server" Text='<%# Eval("Question") %>' meta:resourcekey="QuestionResource1"></asp:Label><br />
                                    <textarea id='<%# Eval("SurveyQuestionTextId") %>' name='<%# Eval("SurveyQuestionTextId") %>' rows="2" cols="20"></textarea>
                                </div>
                            </li>
                        </ItemTemplate>
                    <FooterTemplate>
                        </ul>
                    </FooterTemplate>
                    </asp:Repeater>
                </div>
            </div>
        <asp:Button  CssClass="send-button" ClientIDMode="Static"  OnClientClick="send_click(this.id)" OnClick="Send_Click" ID="Send" runat="server" Text="Skicka in svar" meta:resourcekey="SendSurvey"/>
        </div>
</asp:Content>